package com.bsoft.gol.hbs.message.sendMsg;

import com.bsoft.gol.hbs.utils.CommonUtils;
import hcn.service.base.sendMsg.ISendMsgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 发送消息实现类工厂
 * @author: zhengcb10708
 * @create: 2020-09-25 14:39
 **/
public class SendMsgServiceImplsFactory {

    //发送消息工具类存储Map
    private final static Map<String, ISendMsgService> SEND_MSG_SERVICE_MAP = new HashMap<>(16);
    private final static Logger logger = LoggerFactory.getLogger(SendMsgServiceImplsFactory.class);

    static {
        /**
         * 初始化消息发送服务实现实例
         */
        logger.info("MessageHandleServiceFactory init start");
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        String[] beanNamesForType = webApplicationContext.getBeanNamesForType(ISendMsgService.class);
        logger.info("beanNamesForType:"+beanNamesForType+"====beanNamesForType.length"+beanNamesForType.length);
        if (beanNamesForType != null && beanNamesForType.length > 0) {
            for (String beanName :
                    beanNamesForType) {
                logger.info("ISendMsgService.beanName:"+beanName);
                ISendMsgService bean = (ISendMsgService) webApplicationContext.getBean(beanName);
                SEND_MSG_SERVICE_MAP.put(bean.getMsgType(), bean);
            }
        }
        logger.info("MessageHandleServiceFactory init finish");
    }

    /**
     * @Describe: 根据传入的msgTemplateMsgTypeList取得对应的工具类
     * @Param: [msgTemplateMsgTypeList]
     * @Return: java.util.List<hcn.service.base.sendMsg.ISendMsgService>
     * @Author: zhengcb10708
     * @Date: 2020/9/25 14:41
     */
    public static List<ISendMsgService> getSendMsgServiceImpls(List<String> msgTemplateMsgTypeList) {
        List<ISendMsgService> sendMsgServiceImpls = new ArrayList<>();
        if (CommonUtils.isNotEmpty(msgTemplateMsgTypeList)) {
            for (String msgTypeKey : msgTemplateMsgTypeList) {
                ISendMsgService sendMsgService = SEND_MSG_SERVICE_MAP.get(msgTypeKey);
                if (null != sendMsgService) {
                    sendMsgServiceImpls.add(sendMsgService);
                }
            }
        }
        return sendMsgServiceImpls;
    }
}
